﻿using System.Collections.Generic;
using System.Windows.Forms;
using Distribution.Service;
using Entities;

namespace DistributionApplication.Controls
{
    public partial class AddRemoveFromDropDown : UserControl
    {
        #region - Properties-

        

        public bool ControlIfEntityExists {get;set;}
        public string Description
        {
            get
            {
                return lblItemDescription.Text;
            }
            set
            {
                lblItemDescription.Text = value;
            }
        }
        public ComboBoxStyle ComboStyle
        {
            get
            {
                return ddlEntites.DropDownStyle;
            } 
            set
            {
                ddlEntites.DropDownStyle = value;    
            }
        }

        #endregion

        #region - Public Methods -

        public AddRemoveFromDropDown()
        {
            InitializeComponent();
            ControlIfEntityExists = true;
            ddlEntites.DropDownStyle = ComboBoxStyle.DropDownList;
        }

        public void LoadEntities(List<ELookUp> entities, string valueMember, string displayMember, string additionalItem)
        {
            MobileUIHelper.FillDropdown(ddlEntites, entities.ToArray(), additionalItem);
        }

        public List<ELookUp> GetSelected()
        {
            var selectedItems = new List<ELookUp>();

            foreach (object item in lstAddedItems.Items)
                selectedItems.Add(item as ELookUp);

            return selectedItems;
        }

        public void Reset()
        {
            lstAddedItems.Items.Clear();

            if (ddlEntites.Items.Count > 0)
                ddlEntites.SelectedIndex = 0;
        }

        public void Load(List<ELookUp> entities)
        {
            foreach (ELookUp entity in entities)
                lstAddedItems.Items.Add(entity);
        }

        #endregion

        #region - Event handlers -

        private void btnAddItem_Click(object sender, System.EventArgs e)
        {
            if (ddlEntites.Text.Trim().Equals(string.Empty))
                return;

            if (ddlEntites.DropDownStyle == ComboBoxStyle.DropDown)
            {
                int itemIndex = SearchItemByText(ddlEntites.Text);
                if (itemIndex < 0)
                {
                    var newTop = new ELookUp();
                    newTop.Type = LookUpType.Top;
                    newTop.Text = ddlEntites.Text;
                    
                    LLookUpService.Insert(newTop);
                    int index = ddlEntites.Items.Add(newTop);
                    ddlEntites.SelectedIndex = index; 
                }
                else
                {
                    ddlEntites.SelectedIndex = itemIndex;
                }
            }

            if (!ControlIfEntityExists)
            {
                if (ddlEntites.SelectedIndex > 0)
                    lstAddedItems.Items.Add(ddlEntites.SelectedItem);
            }
            else
            {
                bool containsItem = lstAddedItems.Items.Contains(ddlEntites.SelectedItem);

                if (ddlEntites.SelectedIndex > 0 && !containsItem)
                    lstAddedItems.Items.Add(ddlEntites.SelectedItem);
            }
        }

        private void btnRemoveItem_Click(object sender, System.EventArgs e)
        {
           if (lstAddedItems.SelectedIndex >= 0)
               lstAddedItems.Items.RemoveAt(lstAddedItems.SelectedIndex);

            //Throw an event to get catched by the main form
        }

        private void ddlEntites_GotFocus(object sender, System.EventArgs e)
        {
            if (ddlEntites.DropDownStyle == ComboBoxStyle.DropDown)
                iplKeyboard.Enabled = true;
        }

        private void ddlEntites_LostFocus(object sender, System.EventArgs e)
        {
            if (ddlEntites.DropDownStyle == ComboBoxStyle.DropDown)
                iplKeyboard.Enabled = false;
        }

        public int SearchItemByText (string text)
        {
            int result = -1;

            for (int i = 0; i < ddlEntites.Items.Count; i++)
            {
                if (ddlEntites.Items[i] is ELookUp && ((ELookUp)ddlEntites.Items[i]).Text.Equals(text))
                {
                    result = i;
                    break;
                }
            }

            return result;
        }
    
        #endregion
    }
}
